﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;

namespace Voluntariar.WEB.Util
{
    public static class Criptografia
    {

        public static string mykey { get; set; }
        public static string DeCifrar(string texto)
        {
            mykey = "Voluntariar";
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
            string sDeCifrar = string.Empty;
            if (texto.Trim().Length > 0)
            {
                des.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(mykey));
                des.Mode = CipherMode.ECB;
                ICryptoTransform desdencrypt = des.CreateDecryptor();
                byte[] buff = Convert.FromBase64String(texto);
                sDeCifrar = ASCIIEncoding.ASCII.GetString(desdencrypt.TransformFinalBlock(buff, 0, buff.Length));
                CaracterEspecial(0, sDeCifrar);
            }
            return sDeCifrar;
        }

        public static string Cifrar(string texto)
        {
            mykey = "Voluntariar";
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
            string sCifrar = string.Empty;
            if (!string.IsNullOrEmpty(texto))
            {
                des.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(mykey));
                des.Mode = CipherMode.ECB;
                ICryptoTransform desdencrypt = des.CreateEncryptor();

                ASCIIEncoding MyASCIIEncoding = new ASCIIEncoding();
                CaracterEspecial(1, texto);
                byte[] buff = ASCIIEncoding.ASCII.GetBytes(texto);
                sCifrar = Convert.ToBase64String(desdencrypt.TransformFinalBlock(buff, 0, buff.Length));
            }
            return sCifrar;
        }

        private static void CaracterEspecial(int Flag, string pTexto)
        {

            //Flag = 1 ==>Cifra, Flag = 0 ==> Decifra
            string CaracterEspecial = string.Empty;
            if (string.IsNullOrEmpty(pTexto))
                return;

            if (Flag.Equals(0))
            {
                //a
                pTexto = pTexto.Replace("agudo_a", "á");
                pTexto = pTexto.Replace("crase_a", "à");
                pTexto = pTexto.Replace("til_a", "ã");
                //e
                pTexto = pTexto.Replace("agudo_e", "é");
                pTexto = pTexto.Replace("crase_e", "è");
                //i
                pTexto = pTexto.Replace("agudo_i", "í");
                pTexto = pTexto.Replace("crase_i", "ì");
                //o
                pTexto = pTexto.Replace("agudo_o", "ó");
                pTexto = pTexto.Replace("crase_o", "ò");
                pTexto = pTexto.Replace("til_o", "õ");
                //u
                pTexto = pTexto.Replace("agudo_u", "ú");
                pTexto = pTexto.Replace("crase_u", "ù");
                //A
                pTexto = pTexto.Replace("agudo_A", "Á");
                pTexto = pTexto.Replace("crase_A", "À");
                pTexto = pTexto.Replace("til_A", "Ã");
                //e
                pTexto = pTexto.Replace("agudo_E", "É");
                pTexto = pTexto.Replace("crase_E", "È");
                //i
                pTexto = pTexto.Replace("agudo_I", "Í");
                pTexto = pTexto.Replace("crase_I", "Ì");
                //o
                pTexto = pTexto.Replace("agudo_O", "Ó");
                pTexto = pTexto.Replace("crase_O", "Ò");
                pTexto = pTexto.Replace("til_O", "Õ");
                //u
                pTexto = pTexto.Replace("agudo_U", "Ú");
                pTexto = pTexto.Replace("crase_U", "Ù");
                //cÇ
                pTexto = pTexto.Replace("cedilha_c", "ç");
                pTexto = pTexto.Replace("cedilha_C", "Ç");
            }
            else
            {
                //a
                pTexto = pTexto.Replace("á", "agudo_a");
                pTexto = pTexto.Replace("à", "crase_a");
                pTexto = pTexto.Replace("ã", "til_a");
                //e
                pTexto = pTexto.Replace("é", "agudo_e");
                pTexto = pTexto.Replace("è", "crase_e");
                //i
                pTexto = pTexto.Replace("í", "agudo_i");
                pTexto = pTexto.Replace("ì", "crase_i");
                //o
                pTexto = pTexto.Replace("ó", "agudo_o");
                pTexto = pTexto.Replace("ò", "crase_o");
                pTexto = pTexto.Replace("õ", "til_o");
                //u
                pTexto = pTexto.Replace("ú", "agudo_u");
                pTexto = pTexto.Replace("ù", "crase_u");
                //A
                pTexto = pTexto.Replace("Á", "agudo_A");
                pTexto = pTexto.Replace("À", "crase_A");
                pTexto = pTexto.Replace("Ã", "til_A");
                //e
                pTexto = pTexto.Replace("É", "agudo_E");
                pTexto = pTexto.Replace("È", "crase_E");
                //i
                pTexto = pTexto.Replace("Í", "agudo_I");
                pTexto = pTexto.Replace("Ì", "crase_I");
                //o
                pTexto = pTexto.Replace("Ó", "agudo_O");
                pTexto = pTexto.Replace("Ò", "crase_O");
                pTexto = pTexto.Replace("Õ", "til_O");
                //u
                pTexto = pTexto.Replace("Ú", "agudo_U");
                pTexto = pTexto.Replace("Ù", "crase_U");
                //cÇ
                pTexto = pTexto.Replace("ç", "cedilha_c");
                pTexto = pTexto.Replace("Ç", "cedilha_C");
            }
            //Reestrutura
            return;
            //return CaracterEspecial;
        }

    }
}